﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FarmerStore.Models;
using FarmerStore.ViewModels;

namespace FarmerStore.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class StoreManagerController : Controller
    {
        FarmerStoreEntities storeDB = new FarmerStoreEntities();

        //
        // GET: /StoreManager/

        public ActionResult Index()
        {
            var albums = storeDB.Products
                .Include("Genre").Include("Artist")
                .ToList();

            return View(albums);
        }

        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        // 
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            var viewModel = new StoreManagerViewModel
            {
                Product = new Product(),
                Genres = storeDB.Genres.ToList(),
            };

            return View(viewModel);
        }

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {

                //Save Product
                storeDB.AddToProducts(product);
                storeDB.SaveChanges();

                return RedirectToAction("Index");
            }

            // Invalid – redisplay with errors
            var viewModel = new StoreManagerViewModel
            {
                Product = product,
                Genres = storeDB.Genres.ToList(),
                //Artists = storeDB.c.ToList()
            };

            return View(viewModel);
        }

        //
        // GET: /StoreManager/Edit/5

        public ActionResult Edit(int id)
        {
            var viewModel = new StoreManagerViewModel
            {
                Product = storeDB.Products.Single(a => a.Id == id),
                Genres = storeDB.Genres.ToList(),
            };

            return View(viewModel);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var product = storeDB.Products.Single(a => a.Id == id);

            try
            {
                //Save Product

                UpdateModel(product, "Product");
                storeDB.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                //Error occurred – so redisplay the form

                var viewModel = new StoreManagerViewModel
                {
                    Product = product,
                    Genres = storeDB.Genres.ToList(),
                };

                return View(viewModel);
            }
        }

        //
        // GET: /StoreManager/Delete/5

        public ActionResult Delete(int id)
        {
            var product = storeDB.Products.Single(a => a.Id == id);

            return View(product);
        }

        [HttpPost]
        public ActionResult Delete(int id, string confirmButton)
        {
            var product = storeDB.Products
                .Include("OrderDetails").Include("Carts")
                .Single(a => a.Id == id);

            // For simplicity, we're allowing deleting of albums 
            // with existing orders We've set up OnDelete = Cascade 
            // on the Product->OrderDetails and Product->Carts relationships

            storeDB.DeleteObject(product);
            storeDB.SaveChanges();

            return View("Deleted");
        }
    }
}
